﻿#region Using directives

using System;
using System.Drawing;

#endregion

namespace MetaHeuristics
{
    /// <summary>
    /// Helper class for mathematical operations on <see cref="PointF"/>.
    /// </summary>
    public sealed class PointMath
    {
        private PointMath()
        {}

        /// <summary>
        /// Computes the Euclidian distance between <paramref name="p0"/>
        /// and <paramref name="p1"/>.
        /// </summary>
        /// <param name="p0">source point</param>
        /// <param name="p1">target point</param>
        /// <returns>
        /// Euclidian distance between <paramref name="p0"/>
        /// and <paramref name="p1"/>.
        /// </returns>
        public static float Dist(PointF p0, PointF p1)
        {
            float dx = p0.X - p1.X;
            float dy = p0.Y - p1.Y;
            return (float)Math.Sqrt( dx*dx+ dy*dy);
        }

        public static double GeoToRadian(double f)
        { 
            int deg = (int)f;
            double min = f - deg;
            return Math.PI * (deg + 0.5 * min / 3.0) / 180.0;
        }

        public static double GeoDist(PointF p0, PointF p1)
        {
            const double RRR = 6378.388;

            double q1 = Math.Cos(GeoToRadian(p0.X) - GeoToRadian(p1.X));
            double q2 = Math.Cos(GeoToRadian(p0.Y) - GeoToRadian(p1.Y));
            double q3 = Math.Cos(GeoToRadian(p0.Y) + GeoToRadian(p1.Y));
            double dij = RRR * Math.Acos(0.5 * ((1.0 + q1) * q2 - (1.0 - q1) * q3)) + 1.0;
            return dij;
        }
    }
}
